﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

/// <summary>
/// Summary description for ShoppingCart
/// </summary>
public class Basket
{
    #region Properties

    public List<SanPham> Items { get; private set; }

    #endregion

    #region Singleton Implementation



    // Readonly properties can only be set in initialization or in a constructor  
    public static readonly Basket Instance;

    // The static constructor is called as soon as the class is loaded into memory  
    static Basket()
    {
        // If the cart is not in the session, create one and put it there  
        // Otherwise, get it from the session  
        if (HttpContext.Current.Session["ASPNETShoppingCart"] == null)
        {
            Instance = new Basket();
            Instance.Items = new List<SanPham>();
            HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;
        }
        else
        {
            Instance = (Basket)HttpContext.Current.Session["ASPNETShoppingCart"];
        }
    }

    // A protected constructor ensures that an object can't be created from outside  
    protected Basket() { }

    #endregion

    #region Item Modification Methods
    /** 
     * AddItem() - Adds an item to the shopping  
     */
    public void AddItem(int productId)
    {
        // Create a new item to add to the cart  
        SanPham newItem = new SanPham();

        // If this item already exists in our list of items, increase the quantity  
        // Otherwise, add the new item to the list  
        if (Items.Contains(newItem))
        {
            foreach (SanPham item in Items)
            {
                if (item.Equals(newItem))
                {
                    item.Soluong++;
                    return;
                }
            }
        }
        else
        {
            newItem.Soluong = 1;
            Items.Add(newItem);
        }
    }

    /** 
     * SetItemQuantity() - Changes the quantity of an item in the cart 
     */
    public void SetItemQuantity(int productId, int quantity)
    {
        // If we are setting the quantity to 0, remove the item entirely  
        if (quantity == 0)
        {
            RemoveItem(productId);
            return;
        }

        // Find the item and update the quantity  
        SanPham updatedItem = new SanPham();

        foreach (SanPham item in Items)
        {
            if (item.Equals(updatedItem))
            {
                item.Soluong = quantity;
                return;
            }
        }
    }
    /** 
     * RemoveItem() - Removes an item from the shopping cart 
     */
    public void RemoveItem(int productId)
    {
        //CartItem removedItem = new CartItem(productId);
        //Items.Remove(removedItem);
    }
    #endregion

    #region Reporting Methods
    /** 
     * GetSubTotal() - returns the total price of all of the items 
     *                 before tax, shipping, etc. 
     */
    public decimal GetSubTotal()
    {
        //decimal subTotal = 0;
        //foreach (SanPham item in Items)
        //    subTotal += item..TotalPrice;

        return 10;
    }
    #endregion
}